package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.domain.AdmDockerRegistry;
import com.dss.auto_deploy.dto.AdmDockerRegistryAddDto;
import com.dss.auto_deploy.dto.AdmDockerRegistryPageDto;
import com.dss.auto_deploy.dto.AdmDockerRegistryUpdateDto;
import com.dss.auto_deploy.service.IAdmDockerRegistryService;
import com.dss.auto_deploy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
* docker仓库
* @Description:docker仓库控制层
* @author dongshoushan
* @date 2022-09-22 14:39:03
*/
@RestController
@RequestMapping("/admDockerRegistry")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: docker仓库")
public class AdmDockerRegistryController {

    /**
    * docker仓库Service
    */
    private final  IAdmDockerRegistryService admDockerRegistryService;

    /**
    * 新增docker仓库
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增docker仓库", notes="新增docker仓库")
    public Result insert(@RequestBody @Valid AdmDockerRegistryAddDto addDto) {
        admDockerRegistryService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
    * 修改docker仓库
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改docker仓库", notes="修改docker仓库")
    public Result update(@RequestBody @Valid AdmDockerRegistryUpdateDto updateDto) {
        admDockerRegistryService.update(updateDto);
        return Result.success("修改成功");
    }

    /**
    * 批量删除docker仓库
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除docker仓库", notes="删除docker仓库")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admDockerRegistryService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
    * 分页查询docker仓库
    * @param pageDto
    * @return
    */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询docker仓库", notes="分页查询docker仓库")
    public  Result page(AdmDockerRegistryPageDto pageDto) throws InterruptedException {
        return Result.success(admDockerRegistryService.list(pageDto));
    }

    @ApiOperation(value = "离线安装docker仓库", notes="离线安装docker仓库")
    @PostMapping(value = "install", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result install(
                                @RequestParam(value = "ids") String ids,
                                @RequestParam(value = "tempPath") String tempPath,
                                @RequestPart(value = "registryFile") MultipartFile registryFile,
                                @RequestPart(value = "registryWebFile") MultipartFile registryWebFile,
                                HttpServletRequest request) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admDockerRegistryService.install(ids,tempPath,registryFile,registryWebFile);
        return Result.success();
    }
    /**
     * 查询docker仓库
     * @param
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询docker仓库", notes="查询docker仓库")
    public List<AdmDockerRegistry> getAll(String name){
        return admDockerRegistryService.getAll(name);
    }

}
